package com.erp.servlet;

import com.erp.bean.Evaluate;
import com.erp.service.EvaluateService;
import com.erp.service.impl.EvaluateServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author mtf
 * 评价修改控制器
 */
@WebServlet("/evaluateedit.action")
public class EvaluateEditServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建逻辑对象
        EvaluateService evaluateService = new EvaluateServiceImpl();
        //接收评价编号
        String evaidStr = req.getParameter("evaid");
        //强制类型转换
        int evaid = Integer.valueOf(evaidStr);
        System.out.println(evaid);
        //接收type
        String type = req.getParameter("type");
        //通过评价编号找到评价对象
        Evaluate evaluate = evaluateService.findByEvaId(evaid);
        if (type.equals("1")){
            System.out.println(1);
            //将对象放入作用域
            req.setAttribute("evaluate",evaluate);
            //转发到修改评价页面
            req.getRequestDispatcher("evaluateedit.jsp").forward(req,resp);
        }else if (type.equals("2")){
            System.out.println(2);
            //接收评价内容
            String content = req.getParameter("content");
            Evaluate evaluate1 = new Evaluate();//创建一个对象
            evaluate1.setEvaid(evaid);
            evaluate1.setContent(content);
            //外键
            evaluate1.setSupid(evaluate.getSupid());
            evaluate1.setOid(evaluate.getOid());
            evaluate1.setEvatime(new Timestamp(new Date().getTime()));
            System.out.println(evaluate1);
            //调用修改的方法
            boolean flag = evaluateService.update(evaluate1);
            System.out.println(flag);
            //重定向到评价列表控制器
            resp.sendRedirect("evaluatelist.action");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
